﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace Office_XML_File_Enhancer
{
	/// <summary>
	/// Main form of the application
	/// </summary>
	public partial class Main : Form
	{
		#region Constructors (1) 

		/// <summary>
		/// Initializes a new instance of the <see cref="Main"/> class.
		/// </summary>
		public Main()
		{
			InitializeComponent();
		}

		#endregion Constructors 

		#region Methods (2) 

		// Private Methods (2) 

		/// <summary>
		/// Handles the Click event of the btnFolder control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void btnFolder_Click(object sender, EventArgs e)
		{
			folderBrowserDialog.ShowDialog();
			txtFolder.Text = folderBrowserDialog.SelectedPath;
		}

		/// <summary>
		/// Handles the Click event of the btnRecompress control. Uses 7-Zip (alone) to decompress
		/// and recompress the oxml files.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void btnRecompress_Click(object sender, EventArgs e)
		{
			btnRecompress.Enabled = false;
			var option = chkSubFolder.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
			var files = new List<string>();
			files.AddRange(Directory.GetFiles(txtFolder.Text, "*.docx", option));
			files.AddRange(Directory.GetFiles(txtFolder.Text, "*.docm", option));
			files.AddRange(Directory.GetFiles(txtFolder.Text, "*.xlsx", option));
			files.AddRange(Directory.GetFiles(txtFolder.Text, "*.pptx", option));

			foreach (string file in files)
			{
				btnRecompress.Text = (files.IndexOf(file) + 1)*100/files.Count + "%";
				Refresh();
				var psi = new ProcessStartInfo(Application.StartupPath + "\\7za.exe",
				                               "x \"" + file + "\"" + " -o\"" + txtFolder.Text + "\\recompressortemp\" -y")
				          	{
				          		CreateNoWindow = true,
				          		WindowStyle = ProcessWindowStyle.Hidden,
				          		UseShellExecute = false
				          	};
				var result = Process.Start(psi);
				if (result != null)
				{
					result.WaitForExit();
					if (result.HasExited)
					{
						File.Delete(file);
						var psi2 = new ProcessStartInfo(Application.StartupPath + "\\7za.exe",
						                                "a -tzip \"" + file + "\" \"" + txtFolder.Text + "\\recompressortemp\\*\"" +
						                                " -mx9")
						           	{
						           		CreateNoWindow = true,
						           		WindowStyle = ProcessWindowStyle.Hidden,
						           		UseShellExecute = false
						           	};
						var result2 = Process.Start(psi2);
						if (result2 != null)
						{
							result2.WaitForExit();
							if (result2.HasExited)
								Directory.Delete(txtFolder.Text + "\\recompressortemp", true);
						}
					}
				}
			}
			MessageBox.Show("Conversion is completed");
			btnRecompress.Text = "ReCompress";
			btnRecompress.Enabled = true;
		}

		#endregion Methods 
	}
}